//
//  CheckViewController.m
//  项目A --半糖
//
//  Created by romana on 16/5/28.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "CheckViewController.h"
#import "AFNetworking.h"
#import "checkModel.h"
#import "CheckTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "HeadDetailsViewController.h"
#import "checkModel.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface CheckViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *myArray;
@property (nonatomic, strong) UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *myArrayTwo;

@end

@implementation CheckViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 导航设置
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 4, 30)];
    self.navigationItem.titleView = textLabel;
    textLabel.text = @"礼物";
    textLabel.font = [UIFont systemFontOfSize:20];
    //    textLabel.textColor = [UIColor whiteColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:218 / 255.0 green:82 / 255.0 blue:146 / 255.0 alpha:1];
    
    // tableView的创建
    [self creataTabelView];
    // 数据解析
    //    [self dataHandel];
    self.myArray = [NSMutableArray array];
    [self getData];
    [self read];
    
    
    
    
    
    
    
    
    
    // Do any additional setup after loading the view.
}

-(void)read{
    
    self.myArrayTwo = [NSMutableArray array];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *eventArrayPath = [path stringByAppendingPathComponent:@"CheckVieweventArray.txt"];
    NSDictionary *dataDic = [NSDictionary dictionaryWithContentsOfFile:eventArrayPath];
    
    
    NSArray *collectArr = dataDic[@"collections"];
    for (NSDictionary *tempDic in collectArr) {
        checkModel *m = [[checkModel alloc] init];
        [m setValuesForKeysWithDictionary:tempDic];
        [self.myArrayTwo addObject:m];
    }



}

-(void)getData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.liwushuo.com/v2/collections?limit=20&offset=20" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dataDic = responseObject[@"data"];
        //缓存
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        NSString *eventArrayPath = [path stringByAppendingPathComponent:@"CheckVieweventArray.txt"];
        //(2)将字典写入文件
        [dataDic writeToFile:eventArrayPath atomically:YES];
        
        
        NSArray *collectArr = dataDic[@"collections"];
        for (NSDictionary *tempDic in collectArr) {
            checkModel *m = [[checkModel alloc] init];
            [m setValuesForKeysWithDictionary:tempDic];
            [self.myArray addObject:m];
        }
        
        [self.myTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    




}

-(void)creataTabelView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:(UITableViewStylePlain)];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView registerClass:[CheckTableViewCell class] forCellReuseIdentifier:@"tableCell"];
    self.myTableView = tableView;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CheckTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.myArray.count == 0) {
        cell.model = [self.myArrayTwo objectAtIndex:indexPath.row];

    } else {
        cell.model = [self.myArray objectAtIndex:indexPath.row];

    }
    
   // checkModel *m = [self.myArray objectAtIndex:indexPath.row];
//    NSString *str = m.cover_image_url;
//    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:str]];
//    cell.headLabel.text = m.title;
//    cell.txtLable.text = m.subtitle;
    
    tableView.rowHeight = HEIGHT / 3.2;
    cell.imageView.layer.cornerRadius = 10;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.myArray.count == 0) {
        return  self.myArrayTwo.count;
    } else {
        return self.myArray.count;
    }
}
-(void)back{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

// 进入详情(和headCollectionView一个详情)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HeadDetailsViewController *headDVC = [[HeadDetailsViewController alloc] init];
    UINavigationController *headNav = [[UINavigationController alloc] initWithRootViewController:headDVC];
    checkModel *m = [self.myArray objectAtIndex:indexPath.row];
    headDVC.myId = m.checkId;
    [self presentViewController:headNav animated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
